package de.zainodis.triplog.ui;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.IOException;

import de.zainodis.triplog.R;
import de.zainodis.triplog.util.Utilities;

/**
 * Displays an image fullscreen.
 */
public class ImageViewer extends Activity {
    private static final String TAG = "ImageViewer";

    /**
     * Use this as a key in a bundle to pass the image database id (MediaStore) as a long
     */
    public static final String IMAGE_ID = "ImageId";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_viewer);
        //Hide the actionbar so we're fullscreen
        getActionBar().hide();
    }

    @Override
    protected void onResume() {
        super.onResume();
        long id = getIntent().getLongExtra(IMAGE_ID, -1);
        //Load image
        new LoadImageTask().execute(id);
    }

    @Override
    protected void onPause() {
        super.onPause();
        getImageView().setImageBitmap(null);
    }

    private class LoadImageTask extends AsyncTask<Long, Void, Bitmap> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //Show wait spinner
            getProgressBar().setVisibility(View.VISIBLE);
        }

        @Override
        protected Bitmap doInBackground(Long... params) {
            long id = params[0];
            Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(id));
            Bitmap image = null;
            try {
                image = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            } catch (IOException e) {
                Log.e(TAG, "Failed to load image with id " + id, e);
                Toast.makeText(ImageViewer.this, getString(R.string.retrieving_image_failed), Toast.LENGTH_LONG).show();
            }
            return image;
        }

        @Override
        protected void onPostExecute(Bitmap image) {
            super.onPostExecute(image);
            //Hide progressbar and show imageview
            ImageView imageView = (ImageView) getImageView();
            //Set image on imageview
            if (image != null) {
                imageView.setImageBitmap(image);
            }
            imageView.setVisibility(View.VISIBLE);

            getProgressBar().setVisibility(View.GONE);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_image_viewer, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private View getProgressBar() {
        return findViewById(R.id.activity_image_viewer_progressbar);
    }

    private ImageView getImageView() {
        return (ImageView) findViewById(R.id.activity_image_viewer_imageview);
    }
}
